package com.bvgol.example.rabbitmq.listener;/**
 * @Classname OrderListener
 * @Description TODO
 * @Date 2023/3/16 15:48
 * @Created by GUOCHEN
 */

import com.bvgol.example.rabbitmq.constant.Name;
import com.bvgol.example.rabbitmq.entity.OrderEntity;
import com.bvgol.example.rabbitmq.entity.OrderReturnReasonEntity;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @program: my-spring-completely
 * @description:
 * @author: GUOCHEN
 * @create: 2023/03/16 15:48
 */
@Service
@RabbitListener(queues = {Name.queueName})
public class OrderListener {

    @RabbitHandler
    public void receiveMessage(Message message, OrderReturnReasonEntity reasonEntity, Channel channel) throws IOException {
//        System.out.printf("message ->:" + message);
        System.out.println("com.bvgol.example.rabbitmq.listener.OrderListener.receiveMessage(org.springframework.amqp.core.Message, com.bvgol.example.rabbitmq.entity.OrderReturnReasonEntity, com.rabbitmq.client.Channel)");
        System.out.println("reasonEntity = " + reasonEntity);
        //实例启动多个`只有一个消费者能消费到消息
        //只有一个消息被完全的消费完,代码执行完毕,才开始接收下一个,执行下一个
        //deliveryTag 在channel内顺序递增
        //开启手动确认机制
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        //multiple – true 以确认所有消息，包括提供的传递标记;假以仅确认提供的交货标签。批量确认???
        try {
            channel.basicAck(deliveryTag,false);
        } catch (IOException e) {
            channel.basicReject(deliveryTag,true);
            //requeue 是重新入队
            channel.basicNack(deliveryTag,false,true);
            e.printStackTrace();
        }
    }

    @RabbitHandler
    public void receiveMessage(OrderEntity entity, Channel channel) {
        System.out.println("com.bvgol.example.rabbitmq.listener.OrderListener.receiveMessage(com.bvgol.example.rabbitmq.entity.OrderEntity, com.rabbitmq.client.Channel)");
        System.out.println("entity = " + entity.getClass());
    }


//    @RabbitHandler
    //次方法会报错 说参数模棱两可 将object 换成Message 解决了
    public void receiveMessage(Object o,OrderEntity entity) {
        System.out.println("entity = " + entity.getClass());
    }
}
